package cn.zhuizikejithreeig.com.passenger;

import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.ZBarView;

public class SaoMaActivity extends BasicActivity implements QRCodeView.Delegate, View.OnClickListener {
    private ZBarView mQRCodeView;
    private TextView mOpenLight;
    private TextView mCloseLight;

    @Override
    protected boolean actionBarNeedGoon() {
        return false;
    }
    @Override
    public int setContView() {
        return R.layout.activity_sao_ma;
    }
    @Override
    protected int setHandIndexShow() {
        return 1;
    }
    @Override
    protected void initToDo() {
        setTitle("扫码");
        setLeftShow(1,R.mipmap.back,"");
        getLift().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    @Override
    protected void initView() {
        mQRCodeView = (ZBarView) findViewById(R.id.zxingview);
        mQRCodeView.setDelegate(this);
        mOpenLight=(TextView) findViewById(R.id.openlight);
        mCloseLight=(TextView) findViewById(R.id.closelight);
        mOpenLight.setOnClickListener(this);
        mCloseLight.setOnClickListener(this);
    }
    @Override
    protected void netChengedListen(int netMobile) {
    }
    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate(result);
    }
    @Override
    public void onScanQRCodeOpenCameraError() {

    }
    @Override
    protected void onResume() {
        mQRCodeView.startSpot();
        super.onResume();
        //开始扫描二维码
    }

    @Override
    protected void onStart() {
        super.onStart();
        //可以使用相机
        mQRCodeView.startCamera();
        //        mQRCodeView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);
        mQRCodeView.showScanRect();

    }

    @Override
    protected void onStop() {
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onPause() {
        mQRCodeView.stopSpot();
        mQRCodeView.onDestroy();
        super.onPause();

    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }
    private void vibrate(String result) {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
        mQRCodeView.startSpot();
        if (result != null || !TextUtils.isEmpty(result)) {
            mQRCodeView.stopSpot();
            System.out.println("扫码的结果是"+result);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.openlight:
                mQRCodeView.openFlashlight();
                break;
            case R.id.closelight:
                mQRCodeView.closeFlashlight();
                break;
        }
    }
}
